package cn.example.vip.mall.seckill.controller;

import cn.example.vip.mall.util.RespResult;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;

import java.text.SimpleDateFormat;
import java.time.Instant;
import java.util.Date;
import java.util.concurrent.TimeUnit;

//@RestController
//@RequestMapping("/test")
public class RedisLockTest {
	@Autowired
	private RedissonClient redissonClient;
	@Value("${server.port}")
	private String port;
	
	@GetMapping
	public RespResult test() throws InterruptedException {
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		RLock rLock = redissonClient.getLock("No1");
		System.out.println("获取锁:" + simpleDateFormat.format(Date.from(Instant.now())));
		TimeUnit.SECONDS.sleep(5);
		System.out.println("释放锁:" + simpleDateFormat.format(Date.from(Instant.now())));
		rLock.unlock();
		return RespResult.ok(port);
	}
	
}
